var maxPageItemsCount = 15;

var search_conditions = {
	type: "0",
	kw: "",
	no: "",
	start: null,
	end: null
}

function conditions_clear() {
	search_conditions.type = "0";
	search_conditions.kw = "";
	search_conditions.no = "";
	search_conditions.start = null;
	search_conditions.end = null;
}

$(function() {
	get_list();
	$("#search_submit").click(search_ms);
});

function release_ms() {
	window.open("release.html", window);
}

function refresh_list(listinfo) {
	var itemsCount = listinfo.page_items_count;
	if (itemsCount <= 0) {
		show_empty_result();
		return;
	}
	
	itemsCount = Math.min(itemsCount, maxPageItemsCount);
	$("#ms_list").empty();
	$("#ms_list").append("<li class=\"table_head\"><ul><li class=\"message_no\"><div>编号</div></li><li class=\"message_title\"><div>标题</div></li><li class=\"message_type\"><div>类型</div></li><li class=\"message_replied\"><div>是否回复</div></li></ul></li>");
	for (var i = 0; i < itemsCount; i++) {
		var ms = listinfo.messages[i];
		$("#ms_list").append("<li class=\"table_data\"><ul><li class=\"message_no\"><div>" + ms.no + "</div></li><li class=\"message_title\"><div>" + ms.title + "</div></li><li class=\"message_type\"><div>" + ms.type + "</div></li><li class=\"message_replied\"><div>" + ms.replied + "</div></li></ul></li>");
	}
}

function show_empty_result() {
	$("#ms_list").empty();
	$("#ms_list").append("<div>未搜索到相关结果</div>");
}

function get_list() {
	$.post('list.jsp', {'page':'1'}, function(data) {
		conditions_clear();
		json = eval('('+data+')');
		refresh_list(json);
	});
}

function select_type(type) {
	$.post('select.jsp', 'type=' + type + '&page=1', function(data) {
		conditions_clear();
		search_conditions.type = type;
		json = eval('('+data+')');
		refresh_list(json);
	});
}

function search_ms() {
	var kw = $("#search_title").val();
	var no = $("#search_no").val();
	var start = $("#search_start").val();
	var end = $("#search_end").val();
	
	$.post('search.jsp', 'kw=' + kw + '&no=' + no + '&st=' + start + '&en=' + end + '&page=1', function(data) {
		conditions_clear();
		search_conditions.kw = kw;
		search_conditions.no = no;
		search_conditions.start = start;
		search_conditions.end = start;
		json = eval('('+data+')');
		refresh_list(json);
	});
}